package com.readbook.action;


import com.alibaba.fastjson.JSONArray;
import com.readbook.service.WscdhService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/wscdh")
public class wscdh {
    @Resource
    private WscdhService wscdhService;

    @RequestMapping("getScdhNow.html")
    @ResponseBody
    public Map<String, Object> getScdhNow(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("dhsj", df.format(new Date()));
        List<Map<String, Object>> list1 = wscdhService.getScdhNow(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        if (list1.size() > 0) {
            rmap = list1.get(0);
        }
        return rmap;
    }


    @RequestMapping("getScdhTmByPid.html")
    @ResponseBody
    public List<Map<String, Object>> getScdhTmByPid(HttpServletRequest request, HttpServletResponse response) {
        String pid = request.getParameter("pid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("pid", pid);
        List<Map<String, Object>> list1 = wscdhService.getScdhTmByPid(gmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 保存答案
     * @time 2019年9月19日21:22:43
     */
    @RequestMapping("insertBookanswer.html")
    @ResponseBody
    public Map<String, Object> insertBookanswer(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String da = request.getParameter("da");
        Map<String, Object> remap = new HashMap<String, Object>();
        String usid = request.getParameter("usid");
        String username = request.getParameter("username");

        List<Map<String, Object>> listr = (List<Map<String, Object>>) JSONArray.parse(da);
        for (int i = 0; i < listr.size(); i++) {
            Map<String, Object> getmap = listr.get(i);
            String aid = UUID.randomUUID().toString().replaceAll("-", "");
            getmap.put("aid", aid);
            getmap.put("usid", usid);
            getmap.put("username", username);
            int k = wscdhService.insertBookanswer(getmap);
            if (k <= 0) {
                remap.put("msg", "保存失败!");
                remap.put("result", false);
                return remap;
            }
        }
        remap.put("msg", "保存成功!");
        remap.put("result", true);
        return remap;
    }

    /**
     * @author 牟明艳
     * @comments 获取我的诗词大会情况
     * @time 2019年9月20日14:23:26
     */
    @RequestMapping("getUserDtqk.html")
    @ResponseBody
    public Map<String, Object> getUserDtqk(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        String usid = request.getParameter("usid");
        gmap.put("usid", usid);
        List<Map<String, Object>> list1 = wscdhService.getUserDtqk(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        if (list1.size() > 0) {
            rmap = list1.get(0);
            String zql = rmap.get("ZQL").toString();
            int c = Integer.parseInt(zql) * 60;
            int m = 100;
            double n = (double) c / m;
            int zql1 = (int) Math.ceil(n);
            rmap.put("zql1", zql1);
        }
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 获取诗词大会积分排名
     * @time 2019年9月20日14:22:52
     */
    @RequestMapping("getscdhPmBynj.html")
    @ResponseBody
    public Map<String, Object> getscdhPmBynj(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        String usid = request.getParameter("usid");
        gmap.put("usid", usid);

        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        gmap.put("start", startn);
        gmap.put("end", endn);

        List<Map<String, Object>> list1 = wscdhService.getscdhPmBynj(gmap);
        int totalnum = wscdhService.countscdhPmBynj(gmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        rmap.put("totalnum", totalnum);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 查询我是否参加过诗词大会
     * @time 2019年9月20日23:25:18
     */
    @RequestMapping("getmyScdhInfo.html")
    @ResponseBody
    public List<Map<String, Object>> getmyScdhInfo(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        String usid = request.getParameter("usid");
        gmap.put("usid", usid);
        List<Map<String, Object>> list1 = wscdhService.getmyScdhInfo(gmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 查询我最近是否参加过诗词大会
     * @time 2019年9月21日14:33:52
     */
    @RequestMapping("myLastScdhInfo.html")
    @ResponseBody
    public List<Map<String, Object>> myLastScdhInfo(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String usid = request.getParameter("usid");
        gmap.put("usid", usid);
        List<Map<String, Object>> list1 = wscdhService.myLastScdhInfo(gmap);
        return list1;
    }


}
